using System;
using System.Collections.Generic;
using System.Globalization;
using UnityEngine;
using UnityEngine.UI;

namespace MaterialUI
{
	[AddComponentMenu("MaterialUI/Dialogs/Date Picker", 1)]
	public class DialogDatePicker : MaterialDialog
	{
		[SerializeField]
		private Text m_TextDate;

		[SerializeField]
		private Text m_TextMonth;

		[SerializeField]
		private Text m_TextYear;

		[SerializeField]
		private CanvasGroup m_CalendarCanvasGroup;

		[SerializeField]
		private CanvasGroup m_YearCanvasGroup;

		[SerializeField]
		private DialogDatePickerYearList m_DatePickerYearList;

		[SerializeField]
		private Text[] m_DatePickerDayTexts;

		[SerializeField]
		private DialogDatePickerDayItem[] m_DatePickerDayItems;

		[SerializeField]
		private Image m_Header;

		private DateTime m_CurrentDate;

		private DayOfWeek m_DayOfWeek;

		private CultureInfo m_CultureInfo;

		private string m_DateFormatPattern = "ddd, MMM dd";

		private Action<DateTime> m_OnAffirmativeClicked;

		private Action m_OnDismissiveClicked;

		private Vector2 m_InitialSize;

		public Text textDate => m_TextDate;

		public Text textMonth => m_TextMonth;

		public Text textYear => m_TextYear;

		public Image header
		{
			get
			{
				return m_Header;
			}
			set
			{
				m_Header = value;
			}
		}

		public DateTime currentDate
		{
			get
			{
				return m_CurrentDate;
			}
			set
			{
				SetDate(value);
			}
		}

		public DayOfWeek dayOfWeek => m_DayOfWeek;

		public CultureInfo cultureInfo => m_CultureInfo;

		public string dateFormatPattern => m_DateFormatPattern;

		private void Awake()
		{
			m_InitialSize = base.rectTransform.sizeDelta;
		}

		public void Initialize(int year, int month, int day, Action<DateTime> onAffirmativeClicked, Action onDismissiveClicked, Color accentColor)
		{
			SetDate(new DateTime(year, month, day));
			OnDateClicked();
			m_OnAffirmativeClicked = onAffirmativeClicked;
			m_OnDismissiveClicked = onDismissiveClicked;
			SetColor(accentColor);
			Initialize();
			base.rectTransform.sizeDelta = m_InitialSize;
		}

		public void SetColor(Color accentColor)
		{
			m_Header.color = accentColor;
			m_DatePickerYearList.SetColor(accentColor);
			for (int i = 0; i < m_DatePickerDayItems.Length; i++)
			{
				m_DatePickerDayItems[i].selectedImage.color = accentColor;
			}
		}

		public void SetDate(DateTime date)
		{
			m_CurrentDate = date;
			UpdateDaysText();
			UpdateDateList(GetMonthDateList(m_CurrentDate.Year, m_CurrentDate.Month));
			UpdateDatesText();
		}

		public void SetYear(int year)
		{
			DateTime dateTime = default(DateTime);
			dateTime = ((!DateTime.IsLeapYear(year) && m_CurrentDate.Month == 2 && m_CurrentDate.Day == 29) ? new DateTime(year, m_CurrentDate.Month, 28) : new DateTime(year, m_CurrentDate.Month, m_CurrentDate.Day));
			SetDate(dateTime);
			OnDateClicked();
		}

		public void SetDayOfWeek(DayOfWeek dayOfWeek, CultureInfo cultureInfo)
		{
			m_DayOfWeek = dayOfWeek;
			m_CultureInfo = cultureInfo;
			if (m_CultureInfo == null)
			{
				m_CultureInfo = new CultureInfo("en-US");
			}
			UpdateDaysText();
		}

		public void SetDateFormatPattern(string dateFormatPattern)
		{
			m_DateFormatPattern = dateFormatPattern;
			UpdateDatesText();
		}

		private void UpdateDateList(List<DateTime> dateTime)
		{
			for (int i = 0; i < m_DatePickerDayItems.Length; i++)
			{
				DateTime dateTime2 = (i >= dateTime.Count) ? default(DateTime) : dateTime[i];
				m_DatePickerDayItems[i].Init(dateTime2, OnDayItemValueChanged);
				m_DatePickerDayItems[i].UpdateState(m_CurrentDate);
			}
		}

		private void UpdateDatesText()
		{
			m_TextMonth.text = m_CurrentDate.ToString("MMMMM yyyy");
			m_TextYear.text = m_CurrentDate.ToString("yyyy");
			m_TextDate.text = GetFormattedDate(m_CurrentDate);
		}

		private void UpdateDaysText()
		{
			if (m_CultureInfo == null)
			{
				m_CultureInfo = new CultureInfo("en-US");
			}
			for (int i = 0; i < 7; i++)
			{
				int dayofweek = (int)(m_DayOfWeek + i) % 7;
				m_DatePickerDayTexts[i].text = m_CultureInfo.DateTimeFormat.GetDayName((DayOfWeek)dayofweek).Substring(0, 1).ToUpper();
			}
		}

		private void OnDayItemValueChanged(DialogDatePickerDayItem dayItem, bool isOn)
		{
			if (isOn)
			{
				m_CurrentDate = dayItem.dateTime;
				UpdateDatesText();
			}
		}

		public void OnPreviousMonthClicked()
		{
			DateTime currentDate = m_CurrentDate;
			currentDate = currentDate.AddMonths(-1);
			SetDate(new DateTime(currentDate.Year, currentDate.Month, 1));
		}

		public void OnNextMonthClicked()
		{
			DateTime currentDate = m_CurrentDate;
			currentDate = currentDate.AddMonths(1);
			SetDate(new DateTime(currentDate.Year, currentDate.Month, 1));
		}

		private string GetFormattedDate(DateTime date)
		{
			return date.ToString(m_DateFormatPattern, m_CultureInfo);
		}

		private List<DateTime> GetMonthDateList(int year, int month)
		{
			List<DateTime> list = new List<DateTime>();
			DateTime item = new DateTime(year, month, 1);
			while (item.DayOfWeek != m_DayOfWeek)
			{
				item = item.AddDays(-1.0);
			}
			int num = DateTime.DaysInMonth(year, month);
			while (item.Day != num || item.Month != month)
			{
				list.Add(item);
				item = item.AddDays(1.0);
			}
			list.Add(item);
			return list;
		}

		public void OnYearClicked()
		{
			m_YearCanvasGroup.gameObject.SetActive(value: true);
			TweenManager.TweenFloat(delegate(float f)
			{
				m_CalendarCanvasGroup.alpha = f;
			}, m_CalendarCanvasGroup.alpha, 0f, 0.5f);
			TweenManager.TweenFloat(delegate(float f)
			{
				m_YearCanvasGroup.alpha = f;
			}, m_YearCanvasGroup.alpha, 1f, 0.5f, 0.01f);
			m_DatePickerYearList.CenterToSelectedYear(m_CurrentDate.Year);
			m_CalendarCanvasGroup.blocksRaycasts = false;
			m_YearCanvasGroup.blocksRaycasts = true;
			Text textDate = m_TextDate;
			Color color = m_TextDate.color;
			float r = color.r;
			Color color2 = m_TextDate.color;
			float g = color2.g;
			Color color3 = m_TextDate.color;
			textDate.color = new Color(r, g, color3.b, 0.5f);
			Text textYear = m_TextYear;
			Color color4 = m_TextYear.color;
			float r2 = color4.r;
			Color color5 = m_TextYear.color;
			float g2 = color5.g;
			Color color6 = m_TextYear.color;
			textYear.color = new Color(r2, g2, color6.b, 1f);
		}

		public void OnDateClicked()
		{
			TweenManager.TweenFloat(delegate(float f)
			{
				m_CalendarCanvasGroup.alpha = f;
			}, m_CalendarCanvasGroup.alpha, 1f, 0.5f);
			TweenManager.TweenFloat(delegate(float f)
			{
				m_YearCanvasGroup.alpha = f;
			}, m_YearCanvasGroup.alpha, 0f, 0.5f, 0.01f, delegate
			{
				m_YearCanvasGroup.gameObject.SetActive(value: false);
			});
			m_CalendarCanvasGroup.blocksRaycasts = true;
			m_YearCanvasGroup.blocksRaycasts = false;
			Text textDate = m_TextDate;
			Color color = m_TextDate.color;
			float r = color.r;
			Color color2 = m_TextDate.color;
			float g = color2.g;
			Color color3 = m_TextDate.color;
			textDate.color = new Color(r, g, color3.b, 1f);
			Text textYear = m_TextYear;
			Color color4 = m_TextYear.color;
			float r2 = color4.r;
			Color color5 = m_TextYear.color;
			float g2 = color5.g;
			Color color6 = m_TextYear.color;
			textYear.color = new Color(r2, g2, color6.b, 0.5f);
		}

		public void OnButtonOkClicked()
		{
			if (m_OnAffirmativeClicked != null)
			{
				m_OnAffirmativeClicked(m_CurrentDate);
			}
			Hide();
		}

		public void OnButtonCancelClicked()
		{
			if (m_OnDismissiveClicked != null)
			{
				m_OnDismissiveClicked();
			}
			Hide();
		}
	}
}
